package controllayer;
import java.util.ArrayList;

import modellayer.Clerk;
import modellayer.Customer;
import modellayer.Order;
import modellayer.OrderContainer;
import modellayer.PartOrder;
import modellayer.ProductCopy;


/**
 * The class OrderCtrl is to take input from and perform the actions requested from the
 * class SalesUI. These actions are performed on the classes OrderContainer and Order in the modellayer.
 * 
 * @author Allan Eriksen, Christian Stidsborg, Andreas Rye, Casper Madsen, Stefan Bols 
 * @version 051213
 */
public class OrderCtrl
{
	private static OrderCtrl instance;
    private PersonCtrl personCtrl;
    private ProductCtrl productCtrl;
    private CustomerCtrl customerCtrl;
    private OrderContainer orderCon;
    private Order order;
    private Clerk clerk;
    private ProductCopy productCopy;
    private Customer customer;

    /**
     * This constructor initialzes or creates the Person- and Productcontroller. It's also getting
     * the OrderContainer instance.
     */
    private OrderCtrl()
    {
        personCtrl = PersonCtrl.getInstance();
        productCtrl = ProductCtrl.getInstance();
        customerCtrl = CustomerCtrl.getInstance();
        orderCon = OrderContainer.getInstance();
        order = null;
        clerk = null;
        productCopy = null;
        customer = null;
    }
    
    public static OrderCtrl getInstance() {
    	if (instance==null) {
    		instance = new OrderCtrl();
    	}
    	
    	return instance;
    }
    
    /**
     * This method is meant to take a clerk number and find a giving clerk from that.
     * When thats done, we adds that clerk to the order. If anything fails this method
     * will return false, otherwise it will return true.
     * 
     * @param int clerkNo The specific clerks unique ID number.
     * @return boolean For (true)successfully operation or (false)not successfull
     */
    public boolean setClerk(String clerkNo) {
        boolean success = false;
        clerk = personCtrl.getClerk(clerkNo);
        
        if (clerk!=null) {
            success = true;
        }
        
        return success;
    }
    
    /**
     * This method creates a Order object so we can add a Clerk and some products.
     * This method will return a boolean for successfull operation, true for success
     * and false otherwise.
     * 
     * @return boolean For (true)successfully operation or (false)not successfull
     */
    public boolean createOrder() {
        boolean success = false;
        order = new Order(this.clerk);
        
        if (order!=null) {
            orderCon.addOrder(order);
            success = true;
        }
        
        return success;
    }
    
    /**
     * This method adds products to our order. It takes a String barcode as parameter to
     * determined which product there is to be added. The method return a boolean value
     * for successfull operation, true for success and false otherwise.
     * 
     * @param String barcode The unique barcode for the product.
     * @return boolean For (true)successfully operation or (false)not successfull
     */
    public boolean addProduct(String barcode, int numberOfCopies) {
        boolean success = false;
        
        for(int i = 0; i < numberOfCopies; i++) {
	        productCopy = productCtrl.getProductCopy(barcode);
	        if (productCopy!=null) {
	        	order.addProductCopy(productCopy);
	            success = true;
	        }
        }
        
        return success;
    }
    
    /**
     * This method is to be called when a order is done and then is to be closed.
     * All orders HAS to be closed when it's done. The method calls methods in
     * Order and ProductCtrl to remove ProductCopies and for mark Order to be
     * closed.
     */
    public boolean closeOrder() {
        /* Closing order and getting the orderlist (list of sold products)
        ArrayList<ProductCopy> list = order.closeOrder();
        
        // Sending the sold products into the productController to get them removed from stuck
        productCtrl.removeProductCopies(list);
        */
    	boolean closed = order.closeOrder();
    	if (closed) {
			order = null;
			productCopy = null;
			customer = null;
		}

    	return closed;
    }
    
    /**
     * This method returns the unique Order number for the current Order that
     * we have created.
     * 
     * @return Stirng The Order number for the current Order
     */
    public String getOrderNo() {
        return order.getOrderNo();
    }
    
    public ArrayList<Order> getOrderList() {
    	return orderCon.getOrderList();
    }
    
    /**
     * This method return the name of the Clerk that we have set on to the Order.
     * 
     * @return String The Clerks name.
     */
    public String getClerkName() {
        return personCtrl.getClerkFirstName(clerk);
    }
    
    /**
     * This method returns the total price for the Order, inclusive VAT.
     * 
     * @return double The total price for the Order
     */
    public double getOrderPriceTotal() {
        return order.getOrderPriceTotal();
    }
    
    public double getOrderPriceTotalWithoutTAX() {
    	return order.totalPriceWithoutVAT();
    }
    
    public double getOrderPriceTotalTAX() {
    	return order.calculateVAT();
    }

	public void logoutAndClearClerk() {
		this.clerk = null;
	}
	
	public ArrayList<PartOrder> getPartOrderList() {
		return order.getPartOrderList();
	}
	
	public boolean isOrderSet() {
		boolean returnVal = false;
		if (order!=null) {
			returnVal = true;
		}
		
		return returnVal;
	}
	
	public void setCustomerToOrder(String customerNo) {
		Customer customer = customerCtrl.getCustomer(customerNo);
		order.setCustomer(customer);
	}
	
	public void deletePartOrderAndReturnsItsCopies(String productNo) {
		order.deletePartOrderAndReturnsItsCopies(productNo);
	}
}



